<div>
    <div id="addNewsForm" class="form-horizontal">



        <div class="form-group">
            <label class="col-lg-12 control-label">分类</label>
            <div class="col-lg-12">
                <select class="form-control" name="categoryId">
                    {% for item in list %}
                    <option value="{{item.id}}">{{item.name}}</option>
                    {% endfor %}
                </select>

            </div>
        </div>

     


        <div class="form-group">
            <label class="col-lg-12 control-label">标题</label>
            <div class="col-lg-12">
                <input type="text" class="form-control" name="title" placeholder="标题" data-bv-field="title">

            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-12 control-label">封面图片</label>
            <div class="col-lg-12">
                <input id="filezh" name="file-zh[]" type="file" data-bv-field="filezh">
                <input type="hidden" class="form-control" name="imgUrl" id="imgUrl">
                <div id="divImg"></div>
                <script>
                    //初始化fileinput
                    var FileInput = function () {
                        var oFile = new Object();

                        //初始化fileinput控件（第一次初始化）
                        oFile.Init = function (ctrlName, uploadUrl) {
                            var control = $('#' + ctrlName);

                            //初始化上传控件的样式
                            control.fileinput({
                                language: 'zh', //设置语言
                                uploadUrl: uploadUrl, //上传的地址
                                allowedFileExtensions: ['jpg', 'gif', 'png'], //接收的文件后缀
                                showUpload: true, //是否显示上传按钮
                                showCaption: false, //是否显示标题
                                browseClass: "btn btn-primary", //按钮样式     
                                dropZoneEnabled: false, //是否显示拖拽区域
                                //minImageWidth: 50, //图片的最小宽度
                                //minImageHeight: 50,//图片的最小高度
                                //maxImageWidth: 1000,//图片的最大宽度
                                //maxImageHeight: 1000,//图片的最大高度
                                //maxFileSize: 0,//单位为kb，如果为0表示不限制文件大小
                                //minFileCount: 0,
                                //maxFileCount: 10, //表示允许同时上传的最大文件个数
                                enctype: 'multipart/form-data',
                                validateInitialCount: true,
                                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                                msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
                            });

                            //导入文件上传完成之后的事件
                            control.on("fileuploaded", function (event, data, previewId, index) {

                                console.log(data);
                                console.log('ggggggggggggggggggggggggggggggggg')
                                // $("#txt_file").on("fileuploaded", function (event, data, previewId, index) {
                                //$("#myModal").modal("hide");
                                var img = data.response.data;
                                console.log(img);

                                if (img == undefined) {
                                    toastr.error('上传失败');
                                    return;
                                }
                                $("#imgUrl").val(img);
                                $("#divImg").html("<img style='height:50px' src='" + img + "'/>");
                                //1.初始化表格
                                //var oTable = new TableInit();
                                //oTable.Init(data);
                                //$("#div_startimport").show();
                            });
                        }
                        return oFile;
                    };



                    $(function () {

                        var url = '/manage/uploadSingle';
                        var allowedFileExtensions = ['jpg', 'png', 'gif', 'jpeg'];

                        var oFileInput = new FileInput();
                        oFileInput.Init("filezh", url);


                        //fileUpload('jobTable', 'filezh', url, 20, 50000000, allowedFileExtensions);
                    });
                </script>

            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-12 control-label">简介</label>
            <div class="col-lg-12">
                <input type="text" class="form-control" name="discribe" placeholder="简介" data-bv-field="discribe">

            </div>
        </div>


        <div class="form-group">
            <label class="col-lg-12 control-label">内容</label>
            <div class="col-lg-12">

                <textarea id="content" name="content" rows="10" cols="80"> </textarea>
            </div>
        </div>

    </div>
</div>


<script src="/public/adminlte/bower_components/ckeditor/ckeditor.js"></script>

<script>
    $(function () {
        CKEDITOR.replace('content')

        $('#addNewsForm').bootstrapValidator({
            phones: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                title: {
                    message: '排序校验失败',
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        digits: {
                            message: '字段必须是正整数'
                        }
                    }
                },
                imgUrl: {
                    message: '内容校验失败',
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        stringLength: {
                            min: 2,
                            max: 50,
                            message: '长度2-50位字符'
                        }
                    }
                }

            }
        });

    });




    function ajaxSubmitForm() {


        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }

        var title = $('input[name="title"]').val();
        var imgUrl = $('input[name="imgUrl"]').val();
        var discribe = $('input[name="discribe"]').val();
        var categoryId= $('select[name="categoryId"]').val();
        var content = CKEDITOR.instances.content.getData();
        var param = {
            'title': title,
            'imgUrl': imgUrl,
            'discribe': discribe,
            'content': content,
            'categoryId': parseInt(categoryId)
        };
        var url = 'add';

        console.log(param);
        var resultInfo = ajaxPost(url, param, false);
        return resultInfo;
    }
</script>